from rest_framework import serializers

from goods.models import SPUSpecification, SPU


class SPUSpecificationSerializer(serializers.ModelSerializer):
    """
    商品规格序列化器
    """
    # 关联对象嵌套序列化返回，子表嵌套父表返回StringRelatedField返回关联表的str方法的结果
    spu=serializers.StringRelatedField(read_only=True)
    spu_id=serializers.IntegerField()
    class Meta:
        # 指定模型类
        model = SPUSpecification
        fields = ('id','name','spu','spu_id')


class SPUSerializer(serializers.ModelSerializer):
    """
    SPU商品序列化器
    """
    class Meta:
        # 指定模型类
        model = SPU
        fields = ('id', 'name')
